package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.BaseContext;
import com.demo.common.R;
import com.demo.dao.DishDao;
import com.demo.dao.SetmealDao;
import com.demo.dao.ShoppingCartDao;
import com.demo.domain.Dish;
import com.demo.domain.Setmeal;
import com.demo.domain.SetmealDish;
import com.demo.domain.ShoppingCart;
import com.demo.service.SetmealDishService;
import com.demo.service.ShoppingCartService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private DishDao dishDao;

    /**
     * 查询购物车信息
     *
     * @return
     */
    @Override
    public R<List<ShoppingCart>> cartListApi() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //通过用户id获取到购物车所有信息
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> list = this.list(lqw);
        return R.success(list);
    }

    /**
     * 添加商品至购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public R<ShoppingCart> addCartApi(@RequestBody ShoppingCart shoppingCart) {

        //查询用户ID
        Long currentId = BaseContext.getCurrentId();
        //将用户id赋值到购物车的用户id属性上,从而确认购物车为哪个用户的
        shoppingCart.setUserId(currentId);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();

        //判断菜品id时候为null
        if (dishId != null) {
            //不为null，代表添加到是菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //代表添加的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartOne = this.getOne(lqw);
        //判断购物车中是否已存在
        //如果存在那么就在数量上加一
        if (shoppingCartOne != null) {
            Integer number = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(number + 1);
            this.updateById(shoppingCartOne);
        } else {
            //不存在，那么添加进去
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
            shoppingCartOne = shoppingCart;
        }
        return R.success(shoppingCartOne);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public R<String> clean() {
        //通过用户ID进行删除购物车
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        this.remove(lqw);
        return R.success("删除成功");
    }


    @Override
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        //获取用户ID
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart one = this.getOne(lqw);
        Integer number = one.getNumber();
        //修改商品数量
        //判断商品数量是否为0
        one.setNumber(number - 1);
        this.updateById(one);

        if(number ==1){
            //如果为0直接删除该商品
            this.remove(lqw);
        }
        return R.success("修改成功");

    }
}
